<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.productName" placeholder="商品名称" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.typeId" placeholder="分类" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in typeList" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
      </el-select>
      <el-select v-model="listQuery.upper" placeholder="状态" clearable class="filter-item" style="width: 130px">
        <el-option :key="1" label="上架中" :value="1" />
        <el-option :key="0" label="已下架" :value="0" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
      <div style="float: right; margin-top: 20px;">
        <el-switch v-model="detailStatus" active-color="#13ce66" :active-value="1" :inactive-value="0" />
        <span style="font-size: x-small; color: #475669">更多信息</span>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      :cell-style="{'padding-top':'8px', 'padding-bottom':'8px'}"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      :default-sort="{prop: 'productId', order: null}"
      tooltip-effect="light"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" width="100" prop="productId" sortable="custom" fixed>
        <template slot-scope="{row}">
          <span>{{ row.productId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" min-width="100px" max-width="200px" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.productName }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="detailStatus===1" label="商品图片" min-width="140px" align="center">
        <template slot-scope="{row}">
          <el-image v-for="(imgUrl, index) in row.productImgList.slice(0,3)" :key="index" style="max-width: 32px; max-height: 24px" :src="imgUrl" fit="contain" :preview-src-list="row.productImgList" />
          <span v-if="row.productImgList.length>3">...</span>
        </template>
      </el-table-column>
      <el-table-column v-if="detailStatus===1" label="简介" min-width="150px" max-width="250px" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.productDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="detailStatus===1" label="产地省份" max-width="150px" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.productProv }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="detailStatus===1" label="标签" max-width="150px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.tags.replace(/(^\s*)|(\s*$)/g, '') === '' || !row.tags">
            暂无
          </span>
          <span v-else>
            <el-tag v-for="item in row.tags.substr(1).split('#')" effect="plain" type="info" size="mini">{{ item }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="规格属性" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.attributes }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.unit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" min-width="100px" align="center" prop="originPrice" sortable="custom">
        <template slot-scope="{row}">
          <span>￥{{ roundPrice(row.originPrice) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销量" min-width="75px" align="center" prop="sales" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.sales }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" min-width="75px" align="center" show-overflow-tooltip prop="typeId" sortable="custom">
        <template slot-scope="{row}">
          <el-tag type="primary" size="mini">{{ typeMap[row.typeId] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="75px" align="center" prop="upper" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.upper===1" type="success" size="mini">上架中</el-tag>
          <el-tag v-else type="info" size="mini">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center" width="140px" prop="updateTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="210px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-tooltip content="设置规格" placement="top" effect="light" :enterable="enterable" :open-delay="500">
            <el-button plain type="primary" size="small" color="red" @click="handleItems(row)" icon="el-icon-tickets" circle />
          </el-tooltip>
          <el-tooltip content="编辑信息" placement="top" effect="light" :enterable="enterable" :open-delay="500">
            <el-button type="primary" size="small" @click="handleUpdate(row)" icon="el-icon-edit" circle />
          </el-tooltip>
          <el-tooltip v-if="row.upper===0" content="上架" placement="top" effect="light" :enterable="enterable" :open-delay="500">
            <el-button size="small" type="success" @click="toggleUpperOn(row)" icon="el-icon-plus" circle />
          </el-tooltip>
          <el-tooltip v-else content="下架" placement="top" effect="light" :enterable="enterable" :open-delay="500">
            <el-button size="small" type="info" @click="toggleUpperOff(row)" icon="el-icon-minus" circle />
          </el-tooltip>
          <el-tooltip content="删除商品" placement="top" effect="light" :enterable="enterable" :open-delay="500">
            <el-button size="small" type="danger" @click="handleDelete(row.productId)" icon="el-icon-delete" circle />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showProductList1" />

  </div>
</template>

<script>
import { typeList } from '@/api/type'
import { showProductList1, toggleUpper, deleteProduct } from '@/api/product'
import { roundPrice } from '@/utils/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'

export default {
  name: 'Product1',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      detailStatus: 1,
      tableKey: 0,
      list: [],
      total: 0,
      enterable: false,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        typeId: '',
        productName: '',
        upper: ''
      },
      typeList: [],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      }
    }
  },
  computed: {
    typeMap: function() {
      return this.typeList.reduce((acc, cur) => {
        acc[cur.typeId] = cur.typeName
        return acc
      }, {})
    }
  },
  created() {
    this.getTypeList()
    this.showProductList1()
  },
  methods: {
    showProductList1() {
      this.listLoading = true
      showProductList1(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getTypeList() {
      typeList(1).then(response => {
        this.typeList = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showProductList1()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    handleCreate() {
      this.$router.push({
        name: 'ProductEdit1',
        params: { id: 0 }
      })
    },
    handleItems(row) {
      this.$router.push({
        name: 'Item1',
        params: {
          id: row.productId
        }
      })
    },
    handleUpdate(row) {
      this.$router.push({
        name: 'ProductEdit1',
        params: {
          id: row.productId
        }
      })
    },
    toggleUpperOn(row) {
      const data = { 'productId': row.productId, 'upper': row.upper }
      toggleUpper(data).then(() => {
        this.showProductList1()
        this.dialogFormVisible = false
        this.$notify({
          title: 'Success',
          message: '上架商品成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    toggleUpperOff(row) {
      const data = { 'productId': row.productId, 'upper': row.upper }
      toggleUpper(data).then(() => {
        this.showProductList1()
        this.dialogFormVisible = false
        this.$notify({
          title: 'Success',
          message: '下架商品成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(productId) {
      this.$confirm('此操作将不可逆地删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProduct(productId).then(() => {
          this.showProductList1()
          this.dialogFormVisible = false
          this.$notify({
            title: 'Success',
            message: '删除商品成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    roundPrice
  }
}
</script>
